// -----------------------------------------------------------------------
//  <copyright file="ServiceCollectionExtensions.cs" company="OSharp开源团队">
//      Copyright (c) 2014-2018 OSharp. All rights reserved.
//  </copyright>
//  <site>http://www.osharp.org</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2018-12-31 0:45</last-date>
// -----------------------------------------------------------------------


namespace OSharp.AspNetCore;

/// <summary>
/// <see cref="IServiceCollection"/>扩展方法
/// </summary>
public static class ServiceExtensions
{
    #region IServiceCollection

    /// <summary>
    /// 获取<see cref="IWebHostEnvironment"/>环境信息
    /// </summary>
    public static IWebHostEnvironment GetWebHostEnvironment(this IServiceCollection services)
    {
        return services.GetSingletonInstance<IWebHostEnvironment>();
    }

    #endregion

    #region IServiceProvider

    /// <summary>
    /// 获取HttpContext实例
    /// </summary>
    public static HttpContext HttpContext(this IServiceProvider provider)
    {
        IHttpContextAccessor accessor = provider.GetService<IHttpContextAccessor>();
        return accessor?.HttpContext;
    }

    /// <summary>
    /// 当前业务是否处于HttpRequest中
    /// </summary>
    public static bool InHttpRequest(this IServiceProvider provider)
    {
        var context = provider.HttpContext();
        return context != null;
    }

    #endregion
}
